#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

void cutpurse_test()
{
  int i, k, j, pass, tempCoins, numPlayers, coin_bonus;
  struct gameState state;

  for(i = 0; i < MAX_ITERATIONS; i++){
    coin_bonus = 0;
    pass = 0;

    numPlayers = 4;

    init_test_data(&state, numPlayers);

    srand(time(NULL));

    for(k = 0; k < numPlayers; k++){
      // set deck
      state.deckCount[k] = 50;
      for(j = 0; j < state.deckCount[k]; j++){
        state.deck[k][j] = random_card();
      }
    }

    // player 2 gets zero copper
    for(k = 0; k < numPlayers; k++){
      // set hand
      state.handCount[k] = 5;
      for(j = 0; j < state.handCount[k]; j++){
        state.hand[k][j] = random_card();
        while(state.hand[k][j] == copper)
          state.hand[k][j] = random_card();
      }
    }

    // players 3 & 4 get copper as 4th and 5th cards
    for(k = 2; k < numPlayers; k++){
      // set hand
      state.handCount[k] = 5;
      for(j =30; j < state.handCount[k]; j++){
	state.hand[k][j] = copper;
      }
    }

    state.hand[0][4] = cutpurse;

    printf("ITERATION %d\n", i);

    tempCoins = state.coins;

    if(cardEffect(state.hand[0][4], -1, -1, -1, &state, 4, &coin_bonus) != 0)
      printf("Failure, cutpurse effect not returned\n");
    else
      pass++;

    if(state.coins == tempCoins + 2)
      pass++;
    else
      printf("Failure, player did not gain 2 coins\n");

    if(state.handCount[1] != 5)
      printf("Failure, player with no copper lost a card\n");
    else
      pass++;

    for(k = 2; k < numPlayers; k++){
      if(state.discard[k] == 0)
	printf("Failure, player's discard pile is empty\n");
      else
	pass++;

      if(state.handCount[k] == 4)
	pass++;
      else 
	printf("Failure, player's hand count is not 4\n");

      if(card_count(state.hand[k], copper, state.handCount[k]) == 1)
	pass++;
      else
	printf("Failure, player still has more than 1 copper in hand\n");
    }

    if(state.hand[0][4] == cutpurse)
      printf("Failure, player didn't discard cutpurse card\n");
    else
      pass++;

    if(pass == 10)
      printf("Success, all tests passed\n");
  }
}

int main()
{
  cutpurse_test();
  return 0;
}
